<%-- 
    Document   : main
    Created on : 02 2, 12, 6:42:38 PM
    Author     : pauld
--%>

<%@page import="java.io.UnsupportedEncodingException"%>
<%@page import="java.security.NoSuchAlgorithmException"%>
<%@page import="Controller.UserManager"%>
<%@page import="Controller.ProductManager"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page 
    contentType="text/html" pageEncoding="UTF-8"
    import="DBConnection.DBConnector, DBConnection.DBConnectionImpl"
    
%>
<%--<%@page session="true"%>--%>

<!DOCTYPE html>
<html>
    <%
        String user = (String)session.getAttribute("user");
        String name = (String)session.getAttribute("name");
        
        //if the user is logged in check for session integrity
    if( (String)session.getAttribute("user") != null ){
        
        //check session integrity by comparing the hash of the session token
        String currSession = request.getRemoteAddr().concat( session.getId() );
        try{
            if( !UserManager.checkSessionIntegrity(currSession, session) ){
                UserManager.logout(request);
                response.sendRedirect("login.jsp");
            }
        }catch(NoSuchAlgorithmException e1){
            UserManager.logout(request);
            response.sendRedirect("login.jsp");
        }
        catch(UnsupportedEncodingException e1){
            UserManager.logout(request);
            response.sendRedirect("login.jsp");
        }
        catch(Exception e1){
            UserManager.logout(request);
            response.sendRedirect("login.jsp");
        }
        
    }
    %>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
        <link rel="stylesheet" href="css/960_24_col.css" />
        <link rel="stylesheet" href="css/reset.css" />
        <link rel="stylesheet" href="css/text.css" />
        <link rel="stylesheet" href="css/style.css" />
        
        <title>FooBar Online Bookstore | Main Page</title>
    </head>
    <body>
        <div id="bar"></div>
        <!-------------------------------header here----------------------->
            <div id="header-wrap">
                <div id="dashboard" class="container_24">
                    <div id="dashboard-logo" class="grid_7">
                        <a href="index.jsp"><img src="images/logo.png" alt="logo" /></a>
                    </div>
                    <div id="dashboard-menu" class="grid_17">
                        <ul class="menu" id="main-menu">
                            <li><a href="index.jsp">Home</a></li>
                            <li><a href="main.jsp">Products</a>
                                <ul style="display: none">
                                    <li><a href="main.jsp?show=book">Books</a></li>
                                    <li><a href="main.jsp?show=mags">Magazines</a></li>
                                    <li><a href="main.jsp?show=cd">CD</a></li>
                                    <li><a href="main.jsp?show=dvd">DVD</a></li>
                                    <li><a href="main.jsp?show=acc">Accessories</a></li>
                                </ul>
                            </li>
                            <%
                            if( user != null && UserManager.isAdmin(user) == 0 && UserManager.isFinanceManager(user) == 0 && UserManager.isProductManager(user) == 0 ){
                                out.println("<li><a href='myProfile.jsp'>My Profile</a></li>");
                            }
                            else if(UserManager.isAdmin(user) == 1){
                                out.println("<li><a href='admin/adminPanel.jsp'>Admin Panel</a></li>");
                            }
                            else if(UserManager.isFinanceManager(user) == 1){
                                out.println("<li><a href='admin/financePanel.jsp'>Sales Panel</a></li>");
                            }
                            else if(UserManager.isProductManager(user) != 0){
                                out.println("<li><a href='admin/productPanel.jsp'>Product Panel</a></li>");
                            }
                            %>
                            <li><a href="about.jsp">About</a></li>
                            <li><a href="contact.jsp">Contact Us</a></li>
                            <%
                            if(user == null){
                                out.println("<li><a href='login.jsp'>Login</a> | <a href='register.jsp'>Join Now</a></li>");
                            }
                            else{
                                out.println("<li><form action='logout' name='logout' method='post'>"
                                + "<a id='user-header' href='myProfile.jsp'>" + name + "</a>"
                                + "<input type='submit' value='Logout' class='submit-button' />"
                                + "</form></li>");
                            }
                            %>
                        </ul>
                    </div>
                </div>
            </div>
        <!-------------------------------content here----------------------->
        <div id="content-wrap">
            <div id="content" class="container_24">
                <!--div id="search-products" class="grid_24">
                    <form action="search" method="post" class="grid_7 prefix_17">
                        <input type="text" class="form-inputs" name="search" />
                        
                        <input type="submit" class="submit-button" value="Search" />
                    </form>
                </div--!>
                <!--List of all products-->
                <div id="products" class="grid_24">
                    <%
                    //get all products then print it out
                    ResultSet productList = ProductManager.getAllProducts();
                    
                    out.println("<ul id='product-list'>");
                            
                    //show the query result for all the products
                    while ( productList.next() ) {
                        out.println("<li><a href='viewProduct.jsp?id=" + productList.getString(8) + "'>"
                                + "<span>" + productList.getString(2) + "</span>"
                                + "<ul>"
                                + "<li>Genre: " + productList.getString(4) + "</li>"
                                + "<li>Type: " + productList.getString(5) + "</li>"
                                + "<li>Price: Php" + productList.getString(6) + "</li>"
                                + "</ul>"
                                + "</a></li>");
                    }
                    
                    out.println("<ul />");

                    //close the database connection
                    ProductManager.closeDB();
                    %>
                </div>
            </div>
        </div>
        <!-------------------------------footer here----------------------->
        <div id="upper-footer-wrap">
            <div id="upper-footer" class="container_24">
                <div class="grid_7 prefix_17">
                    <ul class="menu">
                        <li><a href="#">top</a></li>
                        <li><a href="index.jsp">home</a></li>
                        <li><a href="contact.jsp">contact us</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div id="lower-footer-wrap">
            <div id="lower-footer" class="container_24">
                <center>
                    <p>FooBar Online Bookstore SECPROG Project</p>
                </center>
            </div>
        </div>
    </body>
</html>
